package com.kd.service.order;


import com.kd.dao.OrderStatsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class OrderStatsServiceImpl implements OrderStatsService{

    @Autowired
    private OrderStatsMapper orderStatsMapper;


    // 每日凌晨1点执行
    @Scheduled(cron = "0 5 0 * * *")
    @Override
    public void autoUpdateDailyStats() {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        String dateStr = yesterday.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String date = yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        orderStatsMapper.updateDailyStats(date,  dateStr);
    }

    // 每月1日凌晨3点执行
    @Scheduled(cron = "0 10 0 1 * *")
    @Override
    public void autoUpdateMonthlyStats() {
        String lastMonthStr = LocalDate.now().minusMonths(1)
                .format(DateTimeFormatter.ofPattern("yyyyMM"));
        orderStatsMapper.insertMonthlyStats(lastMonthStr);  // 调用Mapper方法
    }

    // 每年1月1日凌晨4点执行
    @Scheduled(cron = "0 15 0 1 1 *")
    @Override
    public void autoUpdateAnnualStats() {
        String lastYearStr = String.valueOf(LocalDate.now().getYear()  - 1);
        orderStatsMapper.insertAnnualStats(lastYearStr);  // 调用Mapper方法
    }

    @Override
    public List<OrderStatsMapper.DailyStats> getDailyStats(String month) {

        return orderStatsMapper.getDailyStatsByMonth(month);
    }
    @Override
    public List<OrderStatsMapper.MonthlyStats> getMonthlyStats(String year) {

        return orderStatsMapper.getMonthlyStatsByYear(year);
    }
    @Override
    public List<OrderStatsMapper.AnnualStats> getAnnualStats(String startYear, String endYear) {

        return orderStatsMapper.getAnnualStatsByRange(startYear,  endYear);
    }
}
